/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import bean.session.util.JsfUtil;
import entity.CalendarEntity;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.ejb.Stateless;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Root;
import java.text.ParseException;
import type.Month;

@Stateless
public class CalendarEntityFacade extends AbstractFacade<CalendarEntity> {
    @PersistenceContext(unitName = "MacTeam-warPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public CalendarEntityFacade() {
        super(CalendarEntity.class);
    }
    
    public void createPh(CalendarEntity current) throws Exception {
//        Query query = em.createQuery("SELECT c FROM Calendar c WHERE c.day = " + current.getPublicHoliday().getDate() + " AND c.year = " + current.getPublicHoliday().getYear() 
//                + " AND c.month = '" + current.getPublicHoliday().getMonth() + "' AND c.archived != 'true'");
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
        Root<CalendarEntity> calendarRoot = cq.from(CalendarEntity.class);
        cq.select(calendarRoot);
        cq.where(builder.and(builder.equal(calendarRoot.get("archived"), false),builder.equal(calendarRoot.get("publicHoliday"),current.getPublicHoliday())));
        if (em.createQuery(cq).getResultList().size() > 0)
            throw new Exception("Date selected is already a public hoiliday.");
        else
            em.persist(current);
    }
    
    public DataModel getAllSalesInquiry() {
        Query query = em.createQuery("SELECT c FROM Calendar c WHERE c.archived!= 'true' ORDER BY c.publicHoliday asc");
        return new ListDataModel(query.getResultList());
    }

}
